<template>
  <div class="app-container">
    <el-backtop visibility-height="10"></el-backtop>
    <!--工具栏 -->
    <el-row :gutter="25">
      <el-col :span="25" :xs="24">
        <el-form :model="conditions" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="用户账号" prop="username">
            <el-input v-model="conditions.username" placeholder="请输入用户账号" clearable/>
          </el-form-item>
          <el-form-item label="公司名称" prop="company">
            <el-input v-model="conditions.company" placeholder="请输入公司名称" clearable/>
          </el-form-item>
          <el-form-item label="职位名称" prop="job_name">
            <el-input v-model="conditions.job_name" placeholder="请输入职位名称" clearable/>
          </el-form-item>
          <el-form-item label="简历类型" prop="resume_type">
            <el-select v-model="conditions.resume_type" clearable placeholder="请选择">
              <el-option value="online" label="平台简历"/>
              <el-option value="file" label="简历文件"/>
            </el-select>
          </el-form-item>
          <el-form-item label="审核状态" prop="audit_status">
            <el-select v-model="conditions.audit_status" clearable placeholder="请选择">
              <el-option v-for="option in audit_status_options" :value="option.value" :label="option.label"/>
            </el-select>
          </el-form-item>
          <el-form-item label="投递时间" prop="deliver_time">
            <!--            <el-date-picker-->
            <!--              v-model="conditions.deliver_time"-->
            <!--              type="daterange"-->
            <!--              format="yyyy-MM-DD" value-format="yyyy-MM-DD"-->
            <!--              range-separator="至"-->
            <!--              start-placeholder="开始日期"-->
            <!--              end-placeholder="结束日期"-->
            <!--            >-->
            <!--            </el-date-picker>-->
            <el-date-picker
              v-model="conditions.deliver_time"
              type="datetimerange"
              :picker-options="pickerOptions"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" :loading="loading"
                       @click="query"
            >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" :loading="loading" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-edit" size="mini" :disabled="multiple"
                   :loading="auditLoading"
                   @click="handleDeliverAuditByIds"
        >批量审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-message" size="mini" :disabled="single"
                   @click="handleDeliverProcessById"
        >查看详情
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-message" size="mini" :loading="downloadLoading"
                   @click="handleTableDataExcel"
        >导出数据
        </el-button>
      </el-col>
    </el-row>

    <el-row style="margin-top: 10px">
      <el-table
        v-loading="loading"
        :data="audits"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" align="center"/>
        <el-table-column label="审核编号" prop="id" align="center" width="180"></el-table-column>
        <el-table-column label="投递用户" prop="username" align="center"></el-table-column>
        <el-table-column label="应聘公司" prop="company.company" align="center"></el-table-column>
        <el-table-column label="职位名称" prop="job.job_name" align="center"></el-table-column>
        <el-table-column label="职位薪资" prop="job.job_price" align="center" width="120"></el-table-column>
        <el-table-column label="简历名" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.file !== undefined">{{ scope.row.file.file_name }}</div>
            <div v-else-if="scope.row.resume !== undefined">{{ scope.row.resume.resume_title }}</div>
          </template>
        </el-table-column>
        <el-table-column label="简历类型" prop="location" align="center" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.resume_type === 'online'" type="success">{{ '平台简历' }}</el-tag>
            <el-tag v-else-if="scope.row.resume_type === 'file'" type="warning">{{ '简历文件' }}</el-tag>
            <el-tag v-else type="error">{{ '文件异常！' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" prop="audit_status" align="center" width="140"></el-table-column>
        <el-table-column
          label="投递时间" align="center"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time"></i>
            <span style="margin-left: 10px">{{ scope.row.deliver_time }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="130">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" :loading="auditLoading"
                       @click="handleDeliverAuditByIds(scope.row.id)"
            >审核
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-reading"
                       @click="handleDeliverProcessById(scope.row.id)"
            >详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-dialog
      title="简历审核"
      :visible.sync="auditDialogVisible"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      :before-close="auditDialogClose"
    >
      <div v-loading="innerLoading">
        <el-form ref="audit_form" :model="audit_form" :rules="auditRules" label-width="80px">
          <el-form-item label="审核状态" prop="audit_status">
            <el-select v-model="audit_form.audit_status" clearable>
              <el-option v-for="option in audit_status_options.filter(item => item.label !== '已提交，等待审核')" :value="option.value" :label="option.label"/>
            </el-select>
          </el-form-item>
          <el-form-item label="审核备注" prop="remark">
            <el-input type="textarea" v-model="audit_form.remark" :resize="'none'" maxlength="100"
                      :autosize="{ minRows: 5, maxRows: 5}" show-word-limit
            />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button type="primary" :loading="innerLoading" @click="handleUserDeliverAuditStatus">修 改</el-button>
        <el-button @click="auditDialogClose">取 消</el-button>
      </div>
    </el-dialog>

    <el-row type="flex" justify="center" style="margin-top: 20px">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="conditions.currentPage"
        :page-sizes="[10, 25, 50, 100]"
        :page-size="conditions.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="audits_total"
      >
      </el-pagination>
    </el-row>

    <el-dialog
      title="投递详情"
      :visible.sync="deliverInfoVisible"
      :modal-append-to-body='false'
      :close-on-click-modal="false"
      width="550px"
      append-to-body
      :before-close="handleDeliverInfoDialogClose">
      <div v-loading="deliver_loading">
        <el-timeline :reverse="false">
          <el-timeline-item
            class="item"
            v-for="(activity, index) in activities"
            :timestamp="activity.create_time"
            :key="activity.id"
            placement="top"
            :icon="index === 0 ? 'el-icon-loading': 'el-icon-check'"
            :color="index !== 0 ? '#0bbd87' : ''"
            :type="index === 0 ? 'primary' : ''"
            size="large"
          >
            <el-card v-if="activities.state_content !== null && activities.state_content !== ''" style="border-radius: 10px;width: 400px">
              <div slot="header">备注</div>
              <div>{{ activity.state_content }}</div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import { mapGetters } from 'vuex'
import { msToDate } from '@/utils/format'

export default {
  name: 'Audit',
  data() {
    return {
      loading: false,
      server: window.application.minio_host,
      conditions: {
        username: '',
        audit_status: '',
        deliver_time: [],
        company: '',
        resume_type: '',
        job_name: '',
        currentPage: 1,
        size: 10
      },
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      audit_status_options: [
        {
          value: '1',
          label: '已提交，等待审核'
        },
        {
          value: '2',
          label: '已审核'
        },
        {
          value: '3',
          label: '统计数据中'
        },
        {
          value: '4',
          label: '已通知'
        },
        {
          value: '5',
          label: '已通过'
        },
        {
          value: '6',
          label: '未通过'
        },
        {
          value: '7',
          label: '等待面试'
        },
        {
          value: '8',
          label: '已结束'
        }
      ],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },

      auditDialogVisible: false,
      auditLoading: false,
      auditRules: {
        audit_status: [
          { required: true, message: '审核状态不能为空', trigger: ['blur', 'change'] }
        ]
      },
      audit_form: {
        ids: [],
        audit_status: '',
        remark: ''
      },
      innerLoading: false,

      activities: [],
      deliverInfoVisible: false,
      deliver_loading: false,

      downloadLoading: false,
    }
  },
  mounted() {
    this.query()
  },
  computed: {
    ...mapGetters(['audits', 'audits_total'])
  },
  methods: {
    msToDate,
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    query() {
      this.loading = true
      this.$store.dispatch('resume/getAllResumeAudit', this.conditions)
        .then((resp) => {
        }).catch((error) => {
        this.$message.error(error)
      }).finally(() => {
        setTimeout(() => {
          this.loading = false
        }, 1500)
      })
    },
    reset() {
      this.conditions = {
        username: '',
        audit_status: '',
        deliver_time: [],
        company: '',
        resume_type: '',
        job_name: '',
        currentPage: 1,
        size: 10
      }
      this.query()
    },
    handleSizeChange(val) {
      this.conditions.size = val
      this.query()
    },
    handleCurrentChange(val) {
      this.conditions.currentPage = val
      this.query()
    },
    handleDeliverAuditByIds(id) {
      if (id instanceof PointerEvent) {
        this.$store.dispatch("resume/checkBatchUserDeliverResume", this.ids)
          .then((resp) => {
            if (resp) {
              this.auditDialogVisible = true
              this.audit_form.ids = this.ids
            }
          }).catch((error) => {
          this.$message.error(error)
          this.query()
        })
          .finally(() => {
            setTimeout(() => {
              this.auditLoading = false
            }, 1500)
          })
      } else {
        this.auditLoading = true
        this.$store.dispatch('resume/checkUserDeliverResume', id)
          .then((resp) => {
            if (resp) {
              // 存在投递记录
              this.auditDialogVisible = true
              this.audit_form.ids.push(id)
            }
          }).catch((error) => {
            this.$message.error(error)
            this.query()
          })
          .finally(() => {
            setTimeout(() => {
              this.auditLoading = false
            }, 1500)
          })
      }
    },
    handleUserDeliverAuditStatus() {
      this.innerLoading = true
      this.$store.dispatch('resume/auditUserDeliverResume', this.audit_form)
        .then((resp) => {
          if (resp) this.$message.success("审核成功")
          this.auditDialogClose()
          this.query()
        })
    },
    auditDialogClose() {
      this.auditLoading = false
      this.auditDialogVisible = false
      this.innerLoading = false
      this.audit_form = {
        ids: [],
        audit_status: '',
        remark: ''
      }
      this.$refs.audit_form.resetFields()
    },
    handleDeliverProcessById(id) {
      if (id instanceof PointerEvent) {
        this.sendDeliverProcessRequest(this.ids[0])
      } else {
        this.sendDeliverProcessRequest(id)
      }
    },
    handleDeliverInfoDialogClose() {
      this.deliverInfoVisible = false
      this.activities = []
    },
    sendDeliverProcessRequest(id) {
      this.deliverInfoVisible = true
      this.deliver_loading = true
      this.$store.dispatch("resume/getUserDeliverInfoById", id)
        .then((resp) => {
          this.activities = resp
        }).catch((error) => this.$message.error(error))
        .finally(() => {
          setTimeout(() => {
            this.deliver_loading = false
          },1500)
        })
    },
    handleTableDataExcel() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['审核编号', '投递用户', '应聘公司', '职位名称', '职位薪资','简历名','简历类型','审核状态','投递时间']
        const list = this.audits
        const data = this.formatJson(list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: "审核记录",
          autoWidth: true,
          bookType: 'xlsx'
        })
      })
      this.downloadLoading = false
    },
    formatJson(jsonData) {
      let dataJson = []
      jsonData.forEach(item => {
        let arr = []
        arr.push(item.id)
        arr.push(item.username)
        arr.push(item.company.company)
        arr.push(item.job.job_name)
        arr.push(item.job.job_price)
        if (item.file !== undefined) {
          arr.push(item.file.file_name)
        } else if (item.resume !== undefined) {
          arr.push(item.resume.resume_title)
        } else {
          arr.push("简历数据异常")
        }
        arr.push(item.resume_type)
        arr.push(item.audit_status)
        arr.push(item.deliver_time)
        dataJson.push(arr)
      })
      return dataJson
    }
  },
}
</script>

<style scoped lang="scss">

.item ::v-deep .el-card__header {
  background: linear-gradient(90deg, #f5fcfc 0, #fcfbfa 100%);
}
</style>
